﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TransCend.Model
{
    /// <summary>
    /// 指示当一个实体类的属性的属性值是否为系统自动生成
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false)]
    [Serializable]
    public class AutoGenerateAttribute : Attribute
    {
        /// <summary>
        /// 指示一个实体类的属性的属性值是否需要系统自动生成
        /// </summary>
        public AutoGenerateAttribute() { }

        /// <summary>
        /// 指示一个实体类的属性的属性值是否需要系统自动生成
        /// </summary>
        /// <param name="step"></param>
        public AutoGenerateAttribute(Type type)
        {
            this.GeneratorType = type;
        }

        private IAutoGeneratable _generator;
        public IAutoGeneratable Generator { get { return _generator; } }

        private Type _generatorType;
        public Type GeneratorType
        {
            get { return _generatorType; }
            set
            {
                this._generatorType = value;
                Type[] interfaces = _generatorType.GetInterfaces();
                foreach (Type t in interfaces)
                {
                    if (t.Equals(typeof(IAutoGeneratable)))
                    {
                        this._generator = (IAutoGeneratable)Activator.CreateInstance(_generatorType);
                    }
                }

                if (this._generator == null)
                {
                    throw new Exception(string.Format("{0}未实现接口IAutoGeneratable。", _generatorType.FullName));
                }
            }
        }
    }
}
